<template>
  <div class="content">
    <nut-navbar class="navbar" z-index="5" :clickable="false" fixed safe-area-inset-top>
      <template #content>
        <span>{{ t(String(route.meta.title)) }}</span>
      </template>
      <template #left>
        <img src="/imgs/club/arrow_left.png" alt="" @click="router.go(-1)" class="arrow_left" />
      </template>
      <template #right>
        <span class="save" @click="goToPage">管理员申请</span>
      </template>
    </nut-navbar>

    <!-- 成员 -->
    <div class="member_container">
      <div
        v-for="(member, index) in members"
        :key="member.id"
        class="member_item"
      >
        <div class="member_info">
          <img :src="member.avatar" alt="头像" class="member_avatar" />
          <div class="info">
            <div class="name_section">
              <p class="member_name">{{ member.name }}</p>
              <span
                v-if="member.role"
                class="tag"
                :class="{
                  creator: member.role === 'creator',
                  admin: member.role === 'admin'
                }"
              >
                <em class="tag_text">{{ member.role === 'creator' ? '创建者' : (member.role === 'admin' ? '管理员' : '')  }}</em>
              </span>
            </div>
            <span class="vip_tag" :class="'vip_' + member.vipLevel">
              <!-- VIP{{ member.vipLevel }} -->
              <ImgT :src="member.vipLevel" alt="" />
            </span>
          </div>
        </div>
        <div class="member_actions">
          <span
            v-if="member.role === 'admin'"
            class="action_btn"
            @click="updateRole(index, 'normal',  member.name)"
          >
            取消管理员
          </span>
          <span
            v-if="member.role !== 'admin' && member.role !== 'creator'"
            class="action_btn"
            @click="updateRole(index, 'admin',  member.name)"
          >
            设为管理员
          </span>
          <span class="action_btn" v-if="member.role !== 'creator'" @click="removeMember(index, member.name)">踢出</span>
        </div>
      </div>
    </div>
    <!-- 取消/设置/删除 -->
    <Modal v-model:visible="isShowWin" :title="t(club_title)" @close="">
      <div class="popup_body">
        <div class="info">{{ club_tip }}</div>
        <div class="submit_btn">
          <span class="submit_close" @click="close">取消</span>
          <Btn height="44px" width="147px" class="submit" @click="submitAc">确定</Btn>
        </div>
      </div>
    </Modal>
    <!-- 申请 -->
    <Modal v-model:visible="isShowApply" :title="t(club_title)" @close="">
      <div class="popup_body">
        <div class="info">是否确认申请管理员</div>
        <div class="submit_btn">
          <span class="submit_close" @click="closeApply">取消</span>
          <Btn height="44px" width="147px" class="submit" @click="submitAp">确定</Btn>
        </div>
      </div>
    </Modal>
  </div>
</template>

<script setup lang="ts">
import { useI18n } from "vue-i18n";
import { ref, reactive } from "vue";
import {useRoute, useRouter} from "vue-router";

const route = useRoute();
const router = useRouter();
const { t } = useI18n();

const isShowWin = ref(false);
const club_title = ref('');

// 成员列表
const members = reactive([
  {
    id: 1,
    name: "Frank002",
    avatar: "/imgs/club/member_avatar.png",
    vipLevel: "/imgs/club/vip.png",
    role: "creator",
  },
  {
    id: 2,
    name: "Frank002",
    avatar: "/imgs/club/member_avatar.png",
    vipLevel: "/imgs/club/vip.png",
    role: "admin",
  },
  {
    id: 3,
    name: "Frank002",
    avatar: "/imgs/club/member_avatar.png",
    vipLevel: "/imgs/club/vip.png",
    role: "normal",
  },
]);

const club_tip = ref()
const memberIndex = ref(0)
const memberRole = ref('')
// 更新角色
const updateRole = (index: number, role: "admin" | "normal", name: any) => {
  club_tip.value =  role == 'admin' ? `是否将${name}设置为管理员` : `是否${name}将的管理员取消`
  isShowWin.value = true;
  memberIndex.value = index;
  memberRole.value = role;
};

// 移除成员
const removeMember = (index: number, name: any) => {
  club_tip.value = `是否将${name}踢出俱乐部`;
  memberIndex.value = index;
  isShowWin.value = true;
};

const isShowApply = ref(false);
const apply_title = ref('');

const goToPage = () => {
  isShowApply.value = true
  apply_title.value = '提示'
}
const closeApply = () => {
  isShowApply.value = false
}

const submitAp = () => {
  isShowApply.value = false
}

const submitAc = () => {
  if (members[memberIndex.value].role === "creator") return;
  members[memberIndex.value].role = memberRole.value;
  if(members[memberIndex.value].role !== "creator") {
    members.splice(memberIndex.value, 1)
  }
  isShowWin.value = false
}

const close = () => {
  isShowWin.value = false
}

</script>

<style scoped lang="scss">
.content {
  font-size: 14px;
  margin: 0 0 10px;

  .arrow_left {
    width: 24px;
    height: 24px;
  }
  .save {
    line-height: 32px;
    text-align: center;
    color: #5BD9FF;
  }

  .search_box {
    // width: 347px;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .search_input {
    
    :deep(.square) {
      border-radius: 8px;
      height: 44px;
      box-shadow: 0.5px 0.5px 1px 0px #2E296B inset;
    }
  }

  .search_btn {
    width: 82px;
    height: 44px;
    margin-right: 14px;
    font-size: 14px;
    line-height: 44px;
    text-align: center;
    color: #FFFFFF;
    background: linear-gradient(180deg, #5567FF 0%, #9E1EFF 100%);
    box-shadow: 0.5px 0.5px 1px 0px #9B9EFF inset;
    border-radius: 8px;
  }

  .member_container {
    // padding: 20px;
    width: 347px;
    margin: 20px auto 0;

    .member_item {
      height: 62px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: #131232;
      box-shadow: 0.5px 0.5px 1px 0px #2E296B inset;
      border-radius: 10px;
      padding: 10px 15px;
      margin-bottom: 10px;
      position: relative;

      .member_info {
        display: flex;
        align-items: center;
        

        .member_avatar {
          width: 50px;
          height: 50px;
          border-radius: 50%;
          margin-right: 10px;
        }

        .info {
          display: flex;
          flex-direction: column;

          .name_section {
            display: flex;
            align-items: center;
            gap: 8px;
            

            .member_name {
              font-size: 14px;
              font-weight: bold;
              margin: 0;
              color: #FFFFFF;
            }
            .tag {
              width: 45px;
              height: 36px;
              line-height: 32px;
              font-size: 10px;
              border-radius: 6px;
              color: #fff;
              position: absolute;
              left: 0;
              top: 0;
              z-index: 99;

               .tag_text {
                display: inline-block;
                transform: rotate(-40deg);
               }
            }

            .tag.creator {
              background: url(/imgs/club/cjz.png) no-repeat;
              background-size: 100% 100%;
            }

            .tag.admin {
              background: url(/imgs/club/gly.png) no-repeat;
              background-size: 100% 100%;
            }
          }

          .vip_tag {
            margin-top: 5px;

            img {
              width: 50px;
              height: 20px;
            }
          }
        }
      }

      .member_actions {
        display: flex;
        gap: 10px;

        .action_btn {
          height: 32px;
          line-height: 32px;
          font-size: 12px;
          text-align: center;
          color: #C6C4F5;
          padding: 0 8px;
          background: #282747;
          box-shadow: 0.5px 0.5px 1px 0px #C6C4F5 inset;
          border-radius: 8px;
        }
      }
    }
  }


}
.popup_body {
  color: #fff;
  padding: 10px;

  .info {
    margin-bottom: 10px;
    text-align: center;
    color: #FFFFFF;
    margin: 24px 0;
  }

  .submit_btn {
    margin: 20px 0;
    display: flex;
    justify-content: space-evenly;

    .submit_close {
      width: 147px;
      height: 42px;
      color: #FFFFFF;
      text-align: center;
      line-height: 44px;
      background: #282747;
      box-shadow: 0.5px 0.5px 1px 0px #2E296B inset;
      border-radius: 8px;
    }
  }
}
</style>
